#ifndef YSYSWATCHDOG_H
#define YSYSWATCHDOG_H

#include <QUdpSocket>
#include <QThread>
#include <iostream>
#include <QMap>
#include <QTimer>
using namespace std;
//#include <QNetworkDatagram>
#define DOGHEADSTART 0xf1f1
#define DOGHEADEND 0x1f1f
struct _tagWatchDog
{
    quint16 usStart;
    char srcIp[18];
    char destIp[18];
    int type;
    int size;
    quint16 usEnd;
};
enum _tagWatchDogType
{
    DOG_TYPE_Init,//初始化指令
    DOG_TYPE_LinkError,
    DOG_TYPE_ShakeHand,//握手指令
    DOG_TYPE_Restart,//重启指令
    DOG_TYPE_Close//关闭指令
};
typedef void (*RestartFun)(); // 为回调函数命名
class YSysWatchDog :public QThread
{
    Q_OBJECT
public:
    YSysWatchDog(QObject *parent = 0);
    bool init(const QString &confPath);
    int initUdpGroup();
    void getSessionList();
    int sendShakeHands();
    int sendInit();
    int sendRestart(const char * pIp);
    void setRestartFun(RestartFun pRestart);
public slots:
    void readPendingDatagrams();
protected:
    void run();
    bool processDatagram(QByteArray data);
    qint64 write(const char *data, qint64 len, QString destIp, _tagWatchDogType type);
protected slots:
    void onLatestNews(_tagWatchDog dog);
    void onShakeHands(int num);
    void onTimeout();
signals:
    void restart();
private:
    QUdpSocket* m_pUdpSocket;
    QString m_strLocalIp;
    quint16 m_usLocalPort;
    QString m_strGroupIp;
    quint16 m_usGroupPort;
    QMap<QString,int> m_mapSession;
    RestartFun m_funRestart;
};

#endif // YSYSWATCHDOG_H
